package com.hoyue.interact.controller;


import com.hoyue.classroom.service.CourseService;
import com.hoyue.interact.bean.UserComment;
import com.hoyue.interact.service.CommentService;
import com.hoyue.system.bean.User;
import com.hoyue.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/comment")
public class CommentController {


    private String prefix = "interact";


    @Autowired
    CommentService commentService;
    @Autowired
    UserService userService;
    @Autowired
    CourseService courseService;

    @ResponseBody
    @RequestMapping(value = "/cadd",method = RequestMethod.POST)
    public int insertComment(String content,Integer matterId, Model model) {
        // 查询当前登录的用户信息
        User user = userService.getLoginUser();
        // 话题创建评论信息
        int commentId=commentService.insertComment(content,user.getId(),user.getUserName(),matterId);
        // 将当前评论与话题绑定
        UserComment userComment = new UserComment(user.getId(),matterId,commentId);
        model.addAttribute("id",matterId);

        return commentService.addComment(userComment);
    }

    /**
     * 删除评论
     */
    @ResponseBody
    @RequestMapping(value ="/cdelete",method = RequestMethod.POST)
    public  String delComment(Integer id){
        System.out.println(id);
        String msg = "1";
        try {
            commentService.delComment(id);
            msg = "0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;

    }





}
